package com.gxuwz.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.MainUtil;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.Branch;
import com.gxuwz.pojo.Info;
import com.gxuwz.pojo.SysUser;
import com.gxuwz.service.IBranchService;
import com.gxuwz.service.IInfoService;
import com.gxuwz.service.ISysUserService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;


public class BranchAction extends BaseAction implements Preparable, ModelDriven<Branch>{

	
	protected static final String LIST_JSP = "/WEB-INF/page/branch/branch_list.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/branch/branch_edit.jsp"; 
	protected static final String ADD_JSP = "/WEB-INF/page/branch/branch_add.jsp"; 
	protected static final String ADD_CLASS_JSP = "/WEB-INF/page/branch/branch_add_class.jsp"; 
	protected static final String ADD_MAJOR_JSP = "/WEB-INF/page/branch/branch_add_major.jsp"; 
	protected static final String ADD_ACADEMY_JSP = "/WEB-INF/page/branch/branch_add_academy.jsp"; 
	protected static final String ADD_UNIT_JSP = "/WEB-INF/page/branch/branch_add_unit.jsp"; 
	protected static final String ADD_USER_JSP = "/WEB-INF/page/branch/branch_add_user.jsp"; 
	protected static final String TOPIC_ADD_JSP = "/WEB-INF/page/branch_topic_add.jsp"; 

	@Autowired
	private IBranchService branchService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IInfoService infoService;
	
	
	private Page pageUtil;
	private Branch branch;
	
	private String json;

	
	public void addBranch(){
		String userId = (String)getRequest().getSession().getAttribute("userId");
		branch.setParentId(userId);
		if(branch.getBranchType() != 0){
			Info info = infoService.findById(Integer.valueOf(branch.getSonId()));
			branch.setSonName(info.getInfoName());
			branchService.save(branch);
		}else{
			Gson gson = new Gson();
			List<SysUser> userList =  gson.fromJson(json, new TypeToken<List<SysUser>>(){}.getType());
			for(SysUser u : userList){
				branch = new Branch();
				branch.setParentId(userId);
				branch.setBranchType(0);
				branch.setSonId(u.getUserId());
				branch.setSonName(u.getUserName());
				branchService.save(branch);
			}
		}
		
		return;
	}
	
	public String toAddClass(){
		
		setForwardView(ADD_CLASS_JSP);
		return SUCCESS;
	}
	
	public String toAddMajor(){
		
		setForwardView(ADD_MAJOR_JSP);
		return SUCCESS;
	}
	
	public String toAddAcademy(){
		
		setForwardView(ADD_ACADEMY_JSP);
		return SUCCESS;
	}
	
	public String toAddUnit(){
		
		setForwardView(ADD_UNIT_JSP);
		return SUCCESS;
	}
	
	public String toAddUser(){
		
		setForwardView(ADD_USER_JSP);
		return SUCCESS;
	}

	/**
	 * 模糊查询打开页面
	 */
	public void list(){
		pageUtil = new Page();
		pageUtil.setCode(0);
		pageUtil.setData(branchService.findByEntity(branch));
		pageUtil.setCount(pageUtil.getData().size());
		reJson(pageUtil);
	}
	
	/**
	 * 获得联系人集合
	 */
	public void getBranchSet(){
		branch = new Branch();
		List<Branch> branchList = new ArrayList<Branch>();
		String userId = (String)getRequest().getSession().getAttribute("userId");
		if(MainUtil.noNull(userId)){
			branch.setParentId(userId);
			branchList = branchService.findByExample(branch);
		}
		pageUtil = new Page();
		pageUtil.setCode(0);
		pageUtil.setData(branchList);
		pageUtil.setCount(pageUtil.getData().size());
		reJson(pageUtil);
	}
	
	/**
	 * 获得当前用户的联系人
	 */
	public void getFriend(){
		//获得当前用户的联系人集合
		branch = new Branch();
		List<Branch> branchList = new ArrayList<Branch>();
		String userId = (String)getRequest().getSession().getAttribute("userId");
		if(MainUtil.noNull(userId)){
			branch.setParentId(userId);
			branchList = branchService.findByExample(branch);
		}
		//查询出联系人列表
		List<SysUser> userList = new ArrayList<SysUser>();
		for(Branch bran : branchList){
			List<SysUser> users = null;
			switch (bran.getBranchType()) {
			case 0:
				users = sysUserService.findByProperty("userId", bran.getSonId());
				break;
			case 1:
				 users = sysUserService.findByProperty("userClass", bran.getSonName());
				break;
			case 2:
				users = sysUserService.findByProperty("userMajor", bran.getSonName());
				break;
			case 3:
				users = sysUserService.findByProperty("userAcademy", bran.getSonName());
				break;
			case 4:
				users = sysUserService.findByProperty("userAcademy", bran.getSonName());
				break;
				
			}
			for(SysUser u: users){
				if(!userList.contains(u))
				{
					userList.add(u);
				}
			}

		}
		
		
		pageUtil = new Page();
		pageUtil.setCode(0);
		pageUtil.setData(userList);
		pageUtil.setCount(pageUtil.getData().size());
		reJson(pageUtil);
	}
	
	/**
	 * 获得数据（下拉框）
	 */
	public void getList(){
		reJson(branchService.findByEntity(branch));
		return;
	}
	
	public void del(){
		branchService.remove(branch);
		return;
	}
	
	public void add(){
		branchService.save(branch);
		return;
	}
	
	public void update(){
		branchService.update(branch);
		return;
	}
	
	
	public String toList(){
		setForwardView(LIST_JSP);
		return SUCCESS;
	}


	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	@Override
	public Branch getModel() {
		// TODO Auto-generated method stub
		return branch;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		branch = new Branch();
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}



	

	
	
	
}
